<?php

class Profiler {

    private $_memStart = 0;

    private $_memPeekStart = 0;

    private $_memEnd = 0;

    private $_memPeekEnd = 0;

    private static $isprofile = 0;

    private static $ipaddress = '127.0.0.1';

    function __construct() {
        $this->_memStart = memory_get_usage();
        $this->_memPeekStart = memory_get_peak_usage();
        
        Logs::Debug( "#MEM_ST,NOW:{$this->_memStart},PEAK:{$this->_memPeekStart}" );
    }

    public function printMem() {
        $this->_memEnd = memory_get_usage();
        $this->_memPeekEnd = memory_get_peak_usage();
        
        $memDiff = number_format( $this->_memEnd - $this->_memStart );
        $memPeekDiff = number_format( $this->_memPeekEnd - $this->_memPeekStart );
        
        
        // Logs::Debug( "#MEM,PROTO:$E_PROTO" );
        Logs::Debug( "#MEM,STA:{$this->_memStart},PEAK:{$this->_memPeekStart}" );
        Logs::Debug( "#MEM,END:{$this->_memEnd},PEAK:{$this->_memPeekEnd}" );
        Logs::Debug( "#MEM,DIF:$memDiff,PDIFF:$memPeekDiff" );
        Logs::Debug( "#MEM,LINE_END====================" );
    }

    public function printTime( $playerID, $proto ) {
        $process_time = microtime( true ) - $_SERVER["REQUEST_TIME_FLOAT"];
        $process_time = number_format( $process_time, 5 );
        Logs::Info( '#ENTRY,PID:' . $playerID . ',PROTO:' . $proto . ',TIME:' . $process_time );
    }
    
    /*
     * public static function StartProfiler() {
     * $isprofile = CEnv::GetConfig('profile');
     * if ( $isprofile ) {
     * xhprof_enable();
     * }
     * }
     *
     * public static function EndProfiler() {
     * //$isprofile = CEnv::GetConfig('profile');
     * if ( $isprofile ) {
     * $xhprof_data = xhprof_disable();
     * _WriteLog("profile", PEAR_LOG_DEBUG, "PROFILE", $xhprof_data);
     *
     * $ipaddress = CEnv::GetConfig('ip');
     *
     * $XHPROF_ROOT = '/var/www/html/GV5/Server';
     * $XHPROF_SOURCE_NAME = 'CodeIgniter';
     * include_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_lib.php';
     * include_once $XHPROF_ROOT . '/xhprof_lib/utils/xhprof_runs.php';
     * $xhprof_runs = new XHProfRuns_Default();
     * $run_id = $xhprof_runs->save_run($xhprof_data, $XHPROF_SOURCE_NAME);
     *
     * $message = "<a href='http://$ipaddress/xhprof_html/index.php?run=$run_id&source=$XHPROF_SOURCE_NAME' target='_blank'>xhprof Result</a><br>\n";
     * _WriteLog("profile.html", PEAR_LOG_DEBUG, "PROFILE", $message);
     * }
     * }
     */
}